home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / miscnix.zip / HEAD.C < prev    next >
Text File  |  1988-05-28  |  3KB  |  119 lines

  1. #include <stdio.h>
  2. #define    ERROR    0
  3. #define    MAXINT    32767
  4. #define    LONGMAXINT    0x7fffffff
  5.  
  6. /**    HEAD    Print last several lines of a file.
  7.  
  8.     Usage:    head [-n[lbc]] [file ...]
  9.         where n is the number of lines (l), 512 byte blocks (b), or
  10.         characters (c) from the beginning of the file.
  11.  
  12.         One or more files may be given; also, standard input will be
  13.         included in the list if it is a pipe or file.  If more than one
  14.         file is given, the files will be separated by lines of the form:
  15.             ==> file.nam <==
  16.  
  17.     No copyright, dammit:  this is PUBLIC DOMAIN!!!
  18. */
  19.  
  20. int    nlines    =MAXINT;
  21. long    numarg;
  22. char    unit    ='l';
  23.  
  24. #define    BUFLEN    512
  25. char    buf[BUFLEN];
  26. char    *bufpos,*bufend;
  27.  
  28. usage() {
  29.     puts("Usage:  head [-#[lbc]] [file ...]");
  30.     puts("where # is the number of lines from the beginning of the file");
  31.     puts("and the file is standard input by default.");
  32.     exit(1);
  33. }
  34.  
  35. processfile(f,s,b)
  36.     FILE *f;
  37.     char *s;    /* file name */
  38.     int b; {    /* >0 if we want to print it */
  39.     int    lleft;
  40.     long    cleft;
  41.     int    len;
  42.  
  43.     if (b>0) {fputs("==> ",stdout); fputs(s,stdout); puts(" <==");}
  44.  
  45.     lleft=nlines;
  46.     cleft=numarg;
  47.  
  48.     while (lleft && cleft) {
  49.         len=fread(buf, 1, cleft<BUFLEN ? (int) cleft : BUFLEN, f);
  50.         if (len==0) break;
  51.         cleft-=len;
  52.         if (unit!='c') {
  53.         bufpos= &buf;
  54.         bufend= &buf+len;
  55.         do {
  56.             bufpos=strnchr(bufpos,bufend-bufpos,'\n');
  57.             if (bufpos >= bufend) break;
  58.             ++bufpos;
  59.         } while (--lleft);
  60.         len=bufpos-&buf;
  61.         }
  62.         fwrite(buf,1,len,stdout);
  63.     }
  64. }
  65.  
  66. main(argc,argv)
  67.     int argc;
  68.     char **argv; {
  69.     int    arg0    =1;
  70.     int    arg;
  71.     FILE    *f;
  72.     char    c, *s;
  73.  
  74.     /* parse numeric argument */
  75.  
  76.     if (argc>1 && argv[1][0]=='-') {
  77.         s=argv[1]+1;
  78.         c=*s;
  79.         if (isdigit(c)) {
  80.         while (isdigit(c)) {
  81.             numarg=numarg*10+(c-'0');
  82.             c= *++s;
  83.         }
  84.         unit=c;
  85.         if (c=='b') {numarg<<=9; unit='c';}
  86.         else if (c!='c' && c!='l') --s;
  87.         ++s;
  88.         }
  89.         if (*s) usage(); 
  90.         ++arg0;
  91.     }
  92.     if (numarg==0) numarg=10;
  93.     if (unit!='c') {nlines=numarg; numarg=LONGMAXINT;}
  94.  
  95.     arg=arg0;
  96.     --argc;
  97.  
  98.     if (!isatty(0)) {
  99.         --arg0;
  100.         processfile(stdin,"Standard input",argc-arg0);
  101.     }
  102.  
  103.     if (arg0>argc) usage();
  104.  
  105.     for (; arg <= argc; ++arg) {
  106.         if (arg>arg0) putchar('\n');
  107.         /* Open file for input */
  108.         s=argv[arg];
  109.         f=fopen(s,"r");
  110.         if (f==ERROR) {
  111.         fputs("Cannot open ",stderr);
  112.         fputs(s,stderr);
  113.         fputs("\n",stderr);
  114.         exit(1);
  115.         }
  116.         processfile(f,s,argc-arg0);
  117.     }
  118. }
  119.